package ACWing.DataStructure.trie树;
//835. Trie字符串统计

import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/12/28 21:31
 */
public class TrieStringStatistics {
    static int[][]son=new int[100010][26];//可以看成存储的节点第一行第二行这样的
    static int[]cnt=new int[100100];
    static int idx;

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            String s=sc.next();
            String x=sc.next();
            char[] str = x.toCharArray();
            if("I".equals(s)){
                insert(str);
            }
            if("Q".equals(s)){
                System.out.println(query(str));
            }
        }
    }
    public static void insert(char[] str){
        int p=0;
        for (int i = 0;i<str.length; i++) {
            int u=str[i]-'a';
            if(son[p][u]==0){
                son[p][u]=++idx;
            }
            p=son[p][u];
        }
        cnt[p]++;//以p这个下标对应的节点结尾的个数
    }
    public static int query(char[] str){
        int p=0;
        for (int i = 0; i < str.length; i++) {
            int u=str[i]-'a';
            if(son[p][u]==0){
                return 0;
            }
            p=son[p][u];
        }
        return cnt[p];
    }
}
